package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
		System.out.println("Please specify parameters: <amount of tests> <file> <format>");
		return;
		}
		int amount = Integer.parseInt(args[0]);
		File file = new File (args[1]);
		String format = args[2];

		if (file.exists()) {
			System.out.println("File " + file + " already exists. Please remove it manually.");
			return;
		}
		
		List<ContactData> contacts = generateRandomContacts(amount);
		if ("csv".equals(format)) {
			saveContactsToCSVfile(contacts, file);
		} else if ("xml".equals(format)) {
			saveContactsToXMLfile (contacts, file);
		} else {
			System.out.println("Unknown format: " + format);
		}
	}

	private static void saveContactsToXMLfile(List<ContactData> contacts, File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contacts", GroupData.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}

	@SuppressWarnings("unchecked")
	public static List<ContactData> loadContactsFromXMLfile(File file) {
		XStream xstream = new XStream();
		xstream.alias("contacts", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}
	
	private static void saveContactsToCSVfile(List<ContactData> contacts, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts){
			writer.write(contact.getFname() + "," + contact.getLname() + "," + contact.getPrimaddress() + "," + contact.getPrimhomephone()
			 + "," + contact.getMobphone() + "," + contact.getWorkphone() + "," + contact.getEmailprim() + "," + contact.getEmailsec()
			  + "," + contact.getBirthday() + "," + contact.getBirthmonth() + "," +  contact.getBirthyear() + "," + contact.getSecaddress()
			   + "," + contact.getSechomephone() + ",!" + "\n");
		}
		writer.close();
		}		

	public static List<ContactData> generateRandomContacts(int amount) {
		List<ContactData> list = new ArrayList<ContactData>();
		for (int i = 0; i < amount; i++) {
			ContactData contact = new ContactData()
			.withFname(generateRandomValue())
			.withLname(generateRandomValue())
			.withPrimaddress(generateRandomString())
			.withPrimhomephone(generateRandomNumber())
			.withMobphone(generateRandomNumber())
			.withWorkphone(generateRandomNumber())
			.withEmailprim(generateRandomEmail())
			.withEmailsec(generateRandomEmail())
			.withBirthday(generateRandomDay())
			.withBirthmonth(generateRandomMonth())
			.withBirthyear(generateRandomYear())
			.withSecaddress(generateRandomString()) 
			.withSechomephone(generateRandomNumber());
			
			list.add(contact);
		}
		return list;
	}
	
	public static List<ContactData> loadContactsFromCSVfile (File file) throws IOException {
		List<ContactData> list = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null) {
			String[] part = line.split(",");
			ContactData contact = new ContactData()
			.withFname(part[0])
			.withLname(part[1])
			.withPrimaddress(part[2])
			.withPrimhomephone(part[2])
			.withMobphone(part[4])
			.withWorkphone(part[5])
			.withEmailprim(part[6])
			.withEmailsec(part[7])
			.withBirthday(part[8])
			.withBirthmonth(part[9])
			.withBirthyear(part[10])
			.withSecaddress(part[11]) 
			.withSechomephone(part[12]);
			list.add(contact);
			line = bufferedReader.readLine();
		}
		bufferedReader.close();
		return list;
	}
	
	
	public static String generateRandomString() {
		Random rnd = new Random();
		if (rnd.nextInt(10) == 0) {
			return "";
		} else {
			return "test" + rnd.nextInt();
		}
	}
	

	public static String generateRandomYear() {
		Random rnd = new Random();
		int y = 1900 + Math.abs(rnd.nextInt())%(1995-1900);
		return "" + y;
	}

	public static String generateRandomMonth() {
		Object[] myArray={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", "-"};
		Random rnd = new Random();
		Object str = myArray[rnd.nextInt(myArray.length)];
		return "" + str;
	}
	
	public static String generateRandomDay() {
		Random rnd = new Random();
		int day = rnd.nextInt(31);
		if (day == 0) {
			return "-";
		} else {
			return "" + day;
		}
	}

	public static String generateRandomEmail() {
		Random rnd = new Random();
		if (rnd.nextInt(5) == 0) {
			return "";
		} else {
			return "test" + rnd.nextInt(100) + "@mail" + rnd.nextInt(100) + ".com";
		}
	}

	public static String generateRandomValue() {
		Random rnd = new Random();
		return "test" + rnd.hashCode();
	}

	public static String generateRandomNumber() {
		Random rnd = new Random();
		if (rnd.nextInt(10) == 0) {
			return "";
		} else {
			return "" + Math.abs(rnd.nextInt());
		}
	}
}
